import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutterapp1/model/ATUser.dart';
import 'dart:convert';
 const MyUser  = "MyUser";
class MyUserTool{
  static void  getInstance(){
    SharedPreferences.setMockInitialValues({});
  }
  static Future <void> saveData(User user) async{
    final Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
    final SharedPreferences prefs = await _prefs;
    var encode = json.encode(user.toJson());
    prefs.setString(MyUser, encode).then((success){
      print(success);
    });
  }
  static Future <User>getData() async{
    final Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
    final SharedPreferences prefs = await _prefs;
    bool res = prefs.containsKey(MyUser);
    if (res == true) {
      Map<String ,dynamic> object = json.decode(prefs.getString(MyUser));
      User user = User.fromJson(object);
      return user;
    }
    return User();
  }
  static Future <bool>delete() async{
    final Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
    final SharedPreferences prefs = await _prefs;
    bool res = prefs.containsKey(MyUser);
    if (res == true) {
      return await prefs.remove(MyUser);
    }
    return false;
  }

  static Future <bool>login() async{
    User user = await getData();
    if (user.token == null){
      return false;
    }
    return true;

  }
//  static void needLogin() {
//    MyUserTool.login().then((success){
//      if (success == false){
//        Navigator.push(
//            context,
//            new MaterialPageRoute(fullscreenDialog: true,
//                builder: (context) => new LoginCtrl(title:"登录",)));
//      }
//    });
//
//  }
}
